﻿Imports System.IO
Imports FileInfoGrabber
Imports System.Xml

Public Class Form1
    Dim fs As FileStream = Nothing
    Dim NsPreFix As String = "my"
    Dim oXml As New System.Xml.XmlDocument
    Dim oNS As XmlNamespaceManager = New XmlNamespaceManager(oXml.NameTable)

    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        OpenFileDialog1.ShowDialog()
        OpenFileDialog1.Filter = "Xml Files|*.xml"
        OpenFileDialog1.Title = "Choose Infopath form XML file:"
        fs = OpenFileDialog1.OpenFile()
        Button1.Enabled = True
        Button2.Enabled = True
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim fig As New FileInfoGrabber.FileInfoGrabber
        Dim output As String = ""
        Dim NsUri As String = "http://schemas.microsoft.com/office/infopath/2003/myXSD/2010-12-13T16:14:52"
        oNS.AddNamespace(NsPreFix, NsUri)

        oNS.AddNamespace(NsPreFix, NsUri)

        If Not (fs Is Nothing) Then
            If fs.Position > 0 Then
                fs.Position = 0
            End If
            oXml.Load(fs)
            Dim submission_id As String = oXml.SelectSingleNode("/my:myFields/my:hidden_submissionID", oNS).InnerText
        End If

        Dim fileXpath As String = "/my:myFields/my:V3_Org_Attach/my:*"
        Dim bindableDT As DataTable = fig.ParseXmlFiles(oXml, oNS, fileXpath)

        DataGridView2.DataSource = bindableDT


    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        fs = Nothing
        Me.Close()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim fig As New FileInfoGrabber.FileInfoGrabber
        Dim output As String = ""
        Dim oXml As XmlDocument = New XmlDocument()
        Dim oNS As XmlNamespaceManager = Nothing
        Dim NsPreFix As String = "my"
        Dim NsUri As String = "http://schemas.microsoft.com/office/infopath/2003/myXSD/2011-01-08T22:34:46"

        Dim fileXpath As String = "/my:myFields/my:Attachments/my:Attachment/my:attachment_file"
        oNS = New XmlNamespaceManager(oXml.NameTable)
        oNS.AddNamespace(NsPreFix, NsUri)
        If Not (fs Is Nothing) Then
            If fs.Position > 0 Then
                fs.Position = 0
            End If
            oXml.Load(fs)
        End If

        Dim bindableDT As DataTable = fig.ParseXmlFiles(oXml, oNS, fileXpath)

        DataGridView2.DataSource = bindableDT

    End Sub
End Class
